<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1 style="display: inline-block" id="title">盘点损益调整</h1>

    <button onclick="javascript:window.history.back();" type="button" class="btn btn-success"
            style="text-align:center;vertical-align:middle;font-size:30px;position:absolute;right:3%;display: inline-block;background-color: transparent;border: none;color: #5c5c5c;outline:none;">
        <i style="-moz-transform:scaleX(-1);-webkit-transform:scaleX(-1);-o-transform:scaleX(-1);transform:scaleX(-1);filter:FlipH;"
           class="glyphicon glyphicon-share-alt"></i><span
            style="margin-left: 5px;font-size: 16px;position: absolute;top: 20%;">返回</span>
    </button>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" class="form-inline">
                        <div class="row">
                            <input type="hidden" th:value="${checkPlanModel.cpnAdjStatus}" id="status">
                            <div class="form-group">
                                <label>
                                    计划编号
                                </label>
                                    <input name="cpnCode" type="text" class="form-control text" maxlength="255"
                                           th:value="${checkPlanModel.cpnCode}" readonly/>

                            </div>
                            <div class="form-group">
                                <label>
                                    盘点内容
                                </label>
                                    <input type="text" class="form-control text" th:value="${checkPlanModel.cpnContent}"
                                           readonly="readonly"/>
                            </div>
                            <div class="form-group">
                                <label>
                                    物料描述
                                </label>
                                    <input name="cdlMatName" type="text" class="form-control text" maxlength="255"
                                           placeholder="请输入物料描述"/>
                            </div>
                            <div class="form-group ">
                                <label>
                                    项目编号
                                </label>
                                    <input name="cdlMatProCode" type="text" class="form-control text"
                                           placeholder="请输入项目编号"/>
                            </div>
                            <div class="form-group">
                                <label>项目名称</label>
                                    <input name="cdlMatProName" type="text" class="form-control text"
                                           placeholder="请输入项目名称"/>

                            </div>
                            <div class="form-group">
                                <label>
                                    损溢状态
                                </label>
                                    <select name="cdlCheckStatus" class="form-control select">
                                        <option value="">全部</option>
                                        <option value="1">盘盈</option>
                                        <option value="2">盘亏</option>
                                    </select>
                            </div>
                            <!--<div class="form-group">
                                <label>仓库</label>
                                    <select name="cdlMatWhiId" type="text" class="form-control select">
                                        <option value="">请选择</option>
                                        <option th:each="type:${warInfo}" th:value="${type.whiId}"
                                                th:text="${type.whiName}"></option>
                                    </select>
                            </div>-->
                            <div class="form-group">
                                <label>
                                    区位
                                </label>
                                    <input name="cdlMatPosition" type="text" class="form-control text"
                                           placeholder="请输入区位"/>
                            </div>
                            <div class="form-group">
                                <!--<a th:href="@{/purchase/check/adjust/export/{cpnId}(cpnId=${checkPlanModel.cpnId})}"
                                   id="btn_downCheckPlan" class="btn btn-info">
                                    <i class="glyphicon glyphicon-export"></i>
                                    导出
                                </a>-->
                                <button type="button" class="btn btn-success" id="btn-search">搜索</button>

                            </div>
                            <div id="toolbar">
                                <button shiro:hasPermission="halinv:halcheckadj:edit"
                                        th:if="${checkPlanModel.cpnAdjStatus!=3}" id="btn_submit" type="button"
                                        class="btn btn-success pull-right">提交盘点损益调整
                                </button>
                                <button shiro:hasPermission="halinv:halcheckadj:edit"
                                        th:if="${checkPlanModel.cpnAdjStatus==3}" id="btn_revoke" type="button"
                                        class="btn btn-primary pull-right">撤销
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body" style="padding-top: 0px">
                    <table id="bootstrap-table" class="table table-bordered " width="100%">
                    </table>
                    <table id="check-progress-table" hidden class="table table-bordered " width="100%">
                        <thead>
                        <tr>
                            <th>
                                计划编号
                            </th>
                            <th>
                                盘点计划
                            </th>
                            <th>
                                盘点结果
                            </th>
                            <th>
                                盘点损益调整
                            </th>
                            <th>
                                损益表
                            </th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr id="check-progress-table-tbody-firstTr"></tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

<!--盘点模态框-->
<div class="modal fade" id="modal-adjust">
    <div class="modal-dialog" style="width:500px;">
        <div class="modal-content">
            <div class="modal-header" style="padding-bottom: 0px;">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">调整</h4>
            </div>
            <div class="modal-body" style="padding-bottom: 5px;padding-top: 0px;">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="box">
                            <div class="box-header">
                            </div>
                            <div class="box-body">
                                <form id="adjustModalForm" class="form-horizontal" role="form">
                                    <div hidden>
                                        <input type="hidden" name="cdlId">
                                    </div>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group" style="height: 60px;">
                                                <label class="form-label col-sm-3" for="cdlPalVal"
                                                       style="margin: 0px;padding: 0px;line-height: 35px ">损益差</label>
                                                <div class="col-sm-9"
                                                     style="margin: 0px;padding: 0px;line-height: 35px;width: 120px;">
                                                    <input type="text" class="form-control"
                                                           style="border-width: 1px; border-radius:5px;border-color:#d2d6de;width:205px;font-size:12px;height:32px;padding: 6px 12px 6px 12px;text-align: center;vertical-align: middle"
                                                           oninput="xyh.inputDecimalThreeHaveNegative(this)"
                                                           id="cdlPalVal" name="cdlPalVal" placeholder="请输入损益差"/>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group" style="height: 160px">
                                                <label class="form-label col-sm-3" for="cdlRemark"
                                                       style="margin: 0px;padding: 0px;line-height: 35px">
                                                    备注
                                                </label>
                                                <div class="col-sm-9"
                                                     style="margin: 0px;padding: 0px;">
                                                    <input type="text" id="cdlRemark" name="cdlRemark"

                                                           class="form-control" maxlength="255"
                                                           style="width: 200px"
                                                           placeholder="请输入备注">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer" style="padding-top: 5px;">
                <button type="button" class="btn btn-info" id="btn-save-adjust">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>

</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var table;

    /**
     *@FunctionName: findWarehouseList
     *@Description: 获取仓库列表
     *@Author: 徐一贺
     *@CreateDate: 2020/3/24 15:31
     *@Phone: 18241927380
     *@Version: 1.0.0
     */
    function findWarehouseList() {
        js.ajax({
            url: ctx + 'common/warehouseInfo/list',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {},
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    $("#queryForm [name='cdlMatWhiId']").empty();
                    $("#queryForm [name='cdlMatWhiId']").append('<option value="">全部</option>');
                    $("#modalForm [name='cdlWhiId']").empty();
                    $("#modalForm [name='cdlWhiId']").append('<option value="">全部</option>');
                    for (var i = 0; i < result.data.rows.length; i++) {
                        $("#queryForm [name='cdlMatWhiId']").append('<option value="' + result.data.rows[i].whiId + '">' + result.data.rows[i].whiName + '</option>');
                        $("#modalForm [name='cdlWhiId']").append('<option value="' + result.data.rows[i].whiId + '">' + result.data.rows[i].whiName + '</option>');
                    }
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        })
    }

    $(function () {
        findWarehouseList();
        //加载列表数据
        table = js.table.init({
            id: 'bootstrap-table',
            url: ctx + "half/halInvManage/inventoryCheck/result/list",
            showColumns: true,
            showExport: false,
            pageSize: 300,
            striped: false,
            uniqueId: "cdlId",
            onLoadSuccess: function () {
                $('#bootstrap-table thead th').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
                $('#bootstrap-table tbody tr:first td').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
            },
            columns: [
                {
                    title: '序号', field: 'cdlId', width: '40',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '区位', field: 'cdlPosition'},
                {title: '物料描述', field: 'cdlMatName'},
                {title: 'SAP物料编码', field: 'cdlMatCode'},
                {title: '项目号', field: 'cdlProCode'},
                {title: '项目文本', field: 'cdlProName'},
                {title: '当前数量', field: 'cdlItyNum'},
                {title: '单位', field: 'cdlUnit'},
                {title: '盘点数量', field: 'cdlNum'},
                {
                    title: '损益差', field: 'cdlPalVal', formatter: function (value, row, index) {
                        return value;
                    },
                    cellStyle: function (value, row, index) {

                        if (value != undefined && value != null) {
                            if (value > 0) {
                                return {css: {"color": "green"}}
                            } else if (value < 0) {
                                return {css: {"color": "red", "font-weight": "900"}}
                            } else {
                                return {css: {"color": "#333333"}}
                            }
                        } else {
                            return {css: {"color": "#333333"}}
                        }
                    }
                },
                {
                    title: '备注', field: 'cdlRemark', formatter: function (value, row, index) {
                        return value;
                    }
                },
                {
                    title: '操作', field: 'cpnId', width: '180',
                    formatter: function (value, row, index) {
                        var htmlText = "";
                        if (hasP('halinv:halcheckadj:edit')&& $("#status").val()!=3) {
                            htmlText += '<button style="margin-top: 5px;margin-left: 5px;width: 60px" class="btn btn-warning btn-xs" onclick="adjust(' + row.cdlId + ')">调整</button>';
                        }
                        return htmlText;
                    }
                }
            ]
        });
        formValidator();
        /**
         * 表格搜索
         */
        $("#btn-search").on('click', function () {
            js.table.search(table);
        });
        /**
         * 调整模态框确认
         */
        $("#btn-save-adjust").on('click', function () {//销毁表单验证
            $("#adjustModalForm").data('bootstrapValidator').destroy();
            //置空表单验证
            $('#adjustModalForm').data('bootstrapValidator', null);
            //重新绑定表单验证
            formValidator();

            //验证通过式提交
            js.validSubmit({
                formId: "adjustModalForm",
                url: ctx + "half/halInvManage/inventoryCheck/adjust/edit",
                data: new FormData($("#adjustModalForm")[0]),
                success: function (result) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                    js.modal.hide("modal-adjust");
                },
                error: function (result) {
                    js.modal.fail(result.msg);
                    js.table.refresh(table);
                    js.modal.hide("modal-adjust");
                }
            });
        });


        /**提交盘点损益调整*/
        $("#btn_submit").on("click", function () {
            js.modal.confirm("尊敬的用户，您确认要提交盘点损益调整吗？", function () {
                $.post(ctx + 'half/halInvManage/inventoryCheck/adjust/submit', {
                    cdlCpnId: [[${checkPlanModel.cpnId}]]
                }, function (result) {
                    if (result.type == 'SUCCESS') {
                        location.href = ctx + "half/halInvManage/inventoryCheck/page";
                    }
                }, 'json')
            });
        });
        /**撤销盘点损益调整*/
        $("#btn_revoke").on("click", function () {
            js.modal.confirm("尊敬的用户，您确认要撤销盘点损益调整吗？", function () {
                $.post(ctx + 'half/halInvManage/inventoryCheck/adjust/revoke', {
                    cdlCpnId: [[${checkPlanModel.cpnId}]]
                }, function (result) {
                    if (result.type == 'SUCCESS') {
                        js.modal.success(result.msg);
                        location.href = ctx + "half/halInvManage/inventoryCheck/page";
                    }
                    if (result.type == 'ERROR') {
                        js.modal.fail(result.msg);
                    }
                }, 'json')
            });
        });

    });

    /**盘点验证*/
    function formValidator() {
        // 验证初始化
        js.validate.init("adjustModalForm", {
            fields: {
                cdlPalVal: {
                    validators: {
                        notEmpty: {message: '请输入损益差'}
                    }
                }
            }
        });
    }


    /**
     *@FunctionName: adjust
     *@Description: 调整显示模态框
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 10:35
     *@Phone: 18241927380
     *@Param: [cdlId] 盘点明细主键
     *@Version: 1.0.0
     */
    function adjust(cdlId) {
        $("#adjustModalForm").data('bootstrapValidator').destroy();
        //置空表单验证
        $('#adjustModalForm').data('bootstrapValidator', null);
        //重新绑定表单验证
        formValidator("checkModalForm");
        var rowData = $("#bootstrap-table").bootstrapTable('getRowByUniqueId', cdlId);
        $("#adjustModalForm").fillData(rowData);
        js.modal.open("modal-adjust");
    }
</script>
</body>
</html>
